package com.kuanter.kuanterauto.activity;


import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 修改车辆
 * 
 * @author soup
 * 
 */
public class TjclActivity extends BaseActivity_NoBar {

	public static final int XZSF = 1;
	public static final int XZPP = 2;
	public static final int XZBX = 3;

	private Button btTopLeft_xgcl = null;  //rl_xgcp
	private Button bt_sheng = null;
	private Button bt_done = null;

	private EditText et_cpnum = null;// 车牌数字
	private EditText city_sym;  //城市代码

	private RelativeLayout rl_xgxh = null;
	private TextView tv_pp = null;// 车型品牌
	private TextView tv_xh = null;// 车型型号

//	private String province = "";// 省份
	private String pp = "";// 车牌品牌
	private String xh = "";// 车牌型号
	
	TextView tvTopTitle;


	static final int DATE_DIALOG_ID = 0;
	private boolean isUpdate=false;
	DataLoader dataLoader=null;
	InputMethodManager m = null;
	String userID;
	
	RelativeLayout rl_xgcp;
	
//	String cp = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ns_tjcl);
		
		
		
		tvTopTitle = (TextView) findViewById(R.id.tvTopTitle);
		tvTopTitle.setText("添加车牌");
		
		rl_xgcp = (RelativeLayout) findViewById(R.id.rl_xgcp);
		rl_xgcp.setBackgroundResource(R.drawable.setting_item_all_corner);
		
		Intent intent = getIntent();
		
//		cp = intent.getStringExtra("cp");
		
		userID = intent.getStringExtra("userID");
		
		dataLoader=new DataLoader(this);

		initView();
		initListener();
		m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	private void initView() {
		btTopLeft_xgcl = (Button) findViewById(R.id.btTopLeft_xgcl);
		bt_sheng = (Button) findViewById(R.id.choose_province);
		bt_sheng.setText("鄂");
		
		bt_done = (Button) findViewById(R.id.bt_done);
		et_cpnum = (EditText) findViewById(R.id.et_cpnum);
		rl_xgxh = (RelativeLayout) findViewById(R.id.rl_xgxh);
		rl_xgxh.setVisibility(View.GONE);
		tv_pp = (TextView) findViewById(R.id.tv_pp);
		tv_xh = (TextView) findViewById(R.id.tv_xh);
		
		city_sym = (EditText) findViewById(R.id.city_sym);
		city_sym.setText("");
		city_sym.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if(!arg1){
					city_sym.setText(city_sym.getText().toString().toUpperCase());
				}
			}
		});
		
		et_cpnum.setKeyListener(new NumberKeyListener() {
			// 0无键盘 1英文键盘 2模拟键盘 3数字键盘
			@Override
			public int getInputType() {
				return 1;
			}
			// 返回允许输入的字符
			@Override
			protected char[] getAcceptedChars() {
				// TODO Auto-generated method stub
				char[] c = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','L','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','l','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};
				return c;
			}
		});
		
		et_cpnum.setSelection(et_cpnum.getText().toString().length());
		
		/*if(!cp.trim().equals("") && !cp.trim().equals("　请选择")) {
			if(cp.length() >= 1) {
				String ch_sf = cp.substring(0, 1);
				bt_sheng.setText(ch_sf);
			}
			
			if(cp.length() >= 2) {
				String eg_cs = cp.substring(1, 2);
				city_sym.setText(eg_cs);
			}
			
			if(cp.length() >= 3) {
				et_cpnum.setText(cp.subSequence(2, cp.length()));
			}
		}*/
	}

	private void initListener() {
		btTopLeft_xgcl.setOnClickListener(this);
		bt_sheng.setOnClickListener(this);
		bt_done.setOnClickListener(this);
		rl_xgxh.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		
		
		
		et_cpnum.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					et_cpnum.setText(et_cpnum.getText().toString().toUpperCase());
				}
			}
		});
		
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		
		MobclickAgent.onPause(this);
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		m.hideSoftInputFromWindow(et_cpnum.getWindowToken(), 0);
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btTopLeft_xgcl:
			m.hideSoftInputFromWindow(et_cpnum.getWindowToken(), 0);
			finish();
			break;
		case R.id.choose_province:// 选择省份
			et_cpnum.setText(et_cpnum.getText().toString().toUpperCase());
			qStartActivityForResult(XzsfActivity.class, null, XZSF);
			break;
		case R.id.bt_done:// 完成
			et_cpnum.setText(et_cpnum.getText().toString().toUpperCase());
//			if(pp.equals("")){
//				showToast("请选择汽车品牌");
//				break;
//			}else if(xh.equals("")){
//				showToast("请选择汽车型号");
//				break;
//			}else 
		    if(bt_sheng.getText().toString().equals("") || bt_sheng.getText().equals("选择省份")){
				showToast("请选择车牌省份");
				break;
			} else if(city_sym.getText().toString().length() != 1) {
				showToast("请输入车牌字母");
				break;
			} else if(et_cpnum.getText().toString().length()!=5){
				showToast("请输入正确的车牌号");
				break;
			} else{
				/*DataLoader addCar = new DataLoader(TjclActivity.this);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				Log.i("bbb", userID + "----------");
				NameValuePair userId = new BasicNameValuePair("userId",userID);
				NameValuePair vehicleBrand = new BasicNameValuePair("vehicleBrand",pp);
				NameValuePair vehicleModels = new BasicNameValuePair("vehicleModels",xh);
				NameValuePair licensePlateNumber = new BasicNameValuePair("licensePlateNumber",province + city_sym.getText().toString() + et_cpnum.getText().toString().substring(0, 1) + et_cpnum.getText().toString().substring(1, 5));
				params.add(userId);
				params.add(vehicleBrand);
				params.add(vehicleModels);
				params.add(licensePlateNumber);
				addCar.DisplayData(MainConstants.HOST_test + HttpConfigAPI.addCar, params, addCarHandler, "", false);*/
				Intent intent = new Intent();
				intent.putExtra("cp", bt_sheng.getText().toString() + city_sym.getText().toString() + et_cpnum.getText().toString().substring(0, 1) + et_cpnum.getText().toString().substring(1, 5));
				setResult(0, intent);
				bt_done.setClickable(false);
				finish();
				
			}
			break;
		case R.id.rl_xgxh:// 选择车型
			et_cpnum.setText(et_cpnum.getText().toString().toUpperCase());
			qStartActivityForResult(ClppActivity.class, null, XZPP);
			break;
		}
	}
	
	Handler addCarHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				try {
					if(StringUtil.isSuccess(json)) {
						JSONObject jo = new JSONObject(json);
						String vehicleId = jo.optString("vehicleId");
						Intent intent = new Intent();
						intent.putExtra("carCard", bt_sheng.getText().toString() + city_sym.getText().toString() + et_cpnum.getText().toString().substring(0, 1) + et_cpnum.getText().toString().substring(1, 5));  //+ "—" 
						intent.putExtra("carInfo_pp", pp);
						intent.putExtra("carInfo_xh", xh);
						intent.putExtra("vehicleId", vehicleId);
						setResult(0, intent);
						finish();
					} 
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				showToast("添加失败，请稍后再试");
			}
			bt_done.setClickable(true);
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		try {
			switch (resultCode) {
			case XZSF:// 返回省份
				String province = data.getStringExtra("province");
				bt_sheng.setText(province);
				break;
			case XZPP:// 选择品牌
				pp = data.getStringExtra("pp");
				xh = data.getStringExtra("xh");
				tv_pp.setText(pp);
				tv_xh.setText(xh);
				break;
			}

		} catch (Exception e) {
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
